<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/11
 * Time: 13:18
 */

namespace app\v2\controller;


use app\common\controller\Checking;
use app\common\controller\Token;
use fast\Redis;
use think\Controller;
use think\Exception;

class Base extends Controller
{
    protected $input;
    protected $userId;
    protected $user;
    protected $redis;
    public function __construct()
    {
        parent::__construct();
        try{
            $jwt= $this->request->cookie('JAVASESSIONID');
            if ( empty( $jwt ) ) {
                throw new Exception( '请先登录' );
            }
            $data = Token::decode( $jwt );
            if ( $data['status'] == false ) {
                throw new Exception( $data['content'] );
            }
            $this->user = $data['content'];
            if ( empty( $this->user->token ) ) {
                throw new Exception( '登录信息已失效,请重新登录' );
            }
            $this->redis=Redis::getRedis();
            $jwtToken = $this->redis->get( 'jwtToken:' . $this->user->id );
            if ( $jwtToken !== $this->user->token ) {
                throw new Exception( '您的账号已在其它地方登陆，若不是本人操作，请注意账号安全！' );
            }
            $this->userId=$this->user->id;
        }catch (Exception $exception){
            Checking::response($exception->getCode(),$exception->getMessage());
        }
    }
}